<?php  

/* 
 * This file tries to test how interchangeable phpolait and yuiolait are. 
 * 
 * For phpolait: require_once("../phpolait/phpolait.php"); 
 * require_once("../phpolait/phpolait/phpolait.php"); 
 * 
 * For yuiolait: 
 */
require_once("../yuiolait/yuiolait.php"); 

if( YUIOLAIT_DEBUG ) {
    require_once("../utl/utl-logit.php"); 
}

class OLAITTestServer { 
    function vpts_test1($msg) { 
        php_logit("OLAITTestServer:vpts_test1:msg=$msg");
        return $msg; 
    } 

    function vpts_test2($msg, $int1, $int2) { 
        php_logit("OLAITTestServer:vpts_test2:msg=$msg,int1=$int1,int2=$int2");
        $res = "$msg/$int1/$int2";
        php_logit("OLAITTestServer:vpts_test2:returning res=$res");
        return $res;
    }

    function vpts_test3($somedict) { 
        php_logit("OLAITTestServer:vpts_test3");
        $res = "";
        foreach ($somedict as $k => $v) {
            php_logit("OLAITTestServer:vpts_test3:k=$k,v=$v");
            $res .= $k .":". $v ." ";
        }
        return $res;
    } 

    function vpts_test4($somedict) { 
        php_logit("OLAITTestServer:vpts_test4");
        $res = array();
        foreach ($somedict as $k => $v) {
            php_logit("OLAITTestServer:vpts_test4:k=$k,v=$v");
            $res[$k] = $v ."-res";
        }
        return $res;
    } 
}

$server = new JSONRpcServer(new OLAITTestServer()); 

?> 
<html> 
<head> 
<?php $server->javascript("OLAITTestClient"); ?> 
<script language="javascript"> 
function syncCall() { 
    alert( 'syncCallback:res=' + OLAITTestClient.vpts_test1('Hello from synchronous call') ); 
} 

/* Asynchronous callback expecting a string result. */
function asCbStr(res, err) { 
    alert('asCbStr:err=' + err + ':res=' + res); 
} 

/* Asynchronous callback expecting a dictionary result. */
function asCbDict(resDict, err) { 
    resStr = "";
    for(var word in resDict) {
        resStr += word + ":" + resDict[word] + "/";
    }
    alert('asCbDict:err=' + err + ':resStr=' + resStr);
}

</script> 
</head> 
<body> 
Phpolait doesn't work in Opera 9.x but yuiolait should.<br><br>

<a href="javascript:OLAITTestClient.vpts_test1('t1', asCbStr);">
    Asynchronous call - one string param
</a><br> 

<a href="javascript:OLAITTestClient.vpts_test2('t2', 98, 99, asCbStr);">
    Asynchronous call - three params: string, int, int; returns concatenation
</a><br> 

<a href="javascript:OLAITTestClient.vpts_test3({'a':1,'b':2,'c':3}, asCbStr);">
    Asynchronous call - one dictionary param, returns concatenation
</a><br> 

<a href="javascript:OLAITTestClient.vpts_test4({'a':1,'b':2,'c':3}, asCbDict);">
    Asynchronous call - one dictionary param, returns dictionary
</a><br><br>

Synchronous calls are not implemented with yuiolait.
The way I thought about implementing them would require locks
in javascript. While this is possible to do (there are implementations
out there), I expect this can be problematic, especially if we allow
multiple outstanding requests. <br>

For me there is no real use case since I don't really use synchronous 
calls. I expect most people are interested in the asynchronous calls 
anyway.<br>

If you are really interested in implementing synchronous calls with 
yuiolait, email me asking for it or - even better - send me a patch !<br>
<a href="javascript:syncCall();">Synchronous call (doesn't work with yuiolait)</a>
<br>

</body> 
</html>
